package com.kgc.listener;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.kgc.api.buylimit.TradeOrderApi;
import com.kgc.bean.OrderInfo;
import com.kgc.bean.OrderItem;
import com.kgc.constant.ActiveMqConstant;
import com.kgc.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.Log;

import java.util.Map;

/**
 * @Author：周申宇 <p>
 * @Date:2021/6/24 13:36
 * <p>
 * @Decription:
 */
@Component
@Slf4j
public class TradeOrderListener {

    @Autowired
    private RedisUtils redisUtils;

    @Reference
    private TradeOrderApi tradeOrderApi;

    /**
     * @Author: zhouyu
     * @Param: [tradeOrder]
     * @Return: void
     * @Description: 指定消息队列，获取生成订单
     */
    @JmsListener(destination = "buy-limit-trade-order",containerFactory = ActiveMqConstant.QUEUE_NAME_lISTENER_FACTORY)
    public void createTradeOrderFromQueue(String mapStr) {

        log.info("+++++++++++++++++++++++++++++++++++++");
        Map map = JSON.parseObject(mapStr,Map.class);

        log.info("----------{}",map);

        // TUDO 获取从生产者的消息。生成订单
        OrderItem orderItem = JSONObject.toJavaObject(JSON.parseObject(map.get("orderItem").toString()),OrderItem.class);
        log.info("",map.get("orderInfo"));
        OrderInfo orderInfo = JSONObject.toJavaObject(JSON.parseObject(map.get("orderInfo").toString()),OrderInfo.class);

        String str = tradeOrderApi.insertTradeOrderFromQueue(orderInfo,orderItem);
        log.info("订单生成状态:{}",str);

        // 生成订单成功，从redis中删除
        redisUtils.del(orderInfo.getOrderId());
    }
}
